package com.ky.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ky.tms.entity.TmsDriver;
import com.ky.tms.entity.TmsUser;
import com.ky.tms.mapper.TmsDriverMapper;
import com.ky.tms.service.ITmsDriverService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ky.tms.util.SecretUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2020-04-14
 */
@Service
public class TmsDriverServiceImpl extends ServiceImpl<TmsDriverMapper, TmsDriver> implements ITmsDriverService {

    @Autowired
    private TmsDriverMapper tmsDriverMapper;

    @Override
    public IPage<TmsDriver> page(String userName, TmsDriver tmsDriver, Long limit, Long pages) {
        IPage<TmsDriver> page = new Page(pages, limit);
        QueryWrapper<TmsDriver> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(tmsDriver.getDriver())){
            wrapper.like("driver", tmsDriver.getDriver());
        }
        if(StringUtils.isNotEmpty(tmsDriver.getCarNo())){
            wrapper.like("car_no", tmsDriver.getCarNo());
        }
        if(StringUtils.isNotEmpty(tmsDriver.getDrIdcard())){
            wrapper.like("dr_idcard", tmsDriver.getDrIdcard());
        }
        wrapper.eq("is_valid", 1);
        wrapper.orderByDesc("create_time");
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public void add(String userName, TmsDriver tmsDriver) {
        tmsDriver.setIsLock(0);
        tmsDriver.setIsValid(1);
        tmsDriver.setCreateTime(LocalDateTime.now());
        tmsDriver.setCreateUser(userName);
        tmsDriverMapper.insert(tmsDriver);
    }

    @Override
    public void delete(String userName, List<Long> ids) {
        QueryWrapper<TmsDriver> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        tmsDriverMapper.delete(queryWrapper);
        //tmsDriverMapper.deleteByIds(userName, LocalDateTime.now(), ids);
    }

    @Override
    public void update(String userName, TmsDriver tmsDriver) {
        tmsDriver.setUpdateTime(LocalDateTime.now());
        tmsDriver.setUpdateUser(userName);
        tmsDriverMapper.updateById(tmsDriver);

    }

    @Override
    public List<TmsDriver> selectList(TmsDriver tmsDriver) {
        List<TmsDriver> result = new ArrayList<>();
        QueryWrapper<TmsDriver> wrapper = new QueryWrapper<>();
        wrapper.like("driver", tmsDriver.getDriver());
        wrapper.eq("is_valid", 1);
        wrapper.eq("is_lock", 0);
        List<TmsDriver> tmsDrivers = tmsDriverMapper.selectList(wrapper);
        boolean isNew = false;
        for(TmsDriver tmsDriver1 : tmsDrivers){
            if(!tmsDriver1.getDriver().equals(tmsDriver.getDriver())){
                isNew = true;
                break;
            }
        }
        if(tmsDrivers.size()<1){
            isNew = true;
        }
        if(isNew){
            result.add(tmsDriver);
        }
        result.addAll(tmsDrivers);
        return result;
    }
}
